package vn.edu.saigontech.ncms.model.bookstore;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.OptimisticLockType;

import vn.edu.saigontech.ncms.model.BaseEntity;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
@Table(name = "ncms_bookstore_title")
@SuppressWarnings("serial")
public class ItemTitle extends BaseEntity {
	
	@Column(name = "title", nullable = false)
	private String title;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "program_id", nullable = false)
	private Program program;

	@ManyToMany
	@JoinTable(name = "ncms_bookstore_title_level", joinColumns = @JoinColumn(name = "title_id"), inverseJoinColumns = @JoinColumn(name = "level_id"))
	@OrderBy("order asc")
	private Set<Level> levels = new HashSet<Level>();

	@Column(name = "active")
	private boolean active;

	@Column(name = "comment")
	private String comment;

	public void addLevel(Level level) {
		this.levels.add(level);
	}
	
	public void resetLevels() {
		this.levels.clear();
	}
	
	public String getLevelsId() {
		StringBuffer buffer = new StringBuffer();
		List<Level> list = new ArrayList<Level>(levels);
		for (int i = 0; i < list.size(); i++) {
			if (i == (list.size() - 1)) {
				buffer.append(list.get(i).getId());
			} else
				buffer.append(list.get(i).getId() + ",");
		}
		return buffer.toString();
	}

	public String getLevelsName() {
		StringBuffer buffer = new StringBuffer();
		List<Level> list = new ArrayList<Level>(levels);
		for (int i = 0; i < list.size(); i++) {
			if (i == (list.size() - 1)) {
				buffer.append(list.get(i).getName());
			} else
				buffer.append(list.get(i).getName() + ", ");
		}
		return buffer.toString();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Set<Level> getLevels() {
		return levels;
	}

	public void setLevels(Set<Level> levels) {
		this.levels = levels;
	}

	public Program getProgram() {
		return program;
	}

	public void setProgram(Program program) {
		this.program = program;
	}

//	public Level getLevel() {
//		return level;
//	}
//
//	public void setLevel(Level level) {
//		this.level = level;
//	}
}